<?php

/**
 * 自定义函数库
 *
 */

/**
 * 加密处理
 */
function genSecretStr($str, $str1, $str2 = null) {
    if (is_string($str) && is_string($str1)) {
        $string = md5($str);
        $str = substr($string, 10);
        $string = md5($str . $str1);
        if (!empty($str2) && is_string($str2)) {
            $str = substr($string, 10);
            return md5($str . $str2);
        } else {
            return $string;
        }
    }
    return false;
}



/**
 * 截取替换函数
 */
function strLength($str, $length = 3, $replace = null) {
    if (empty($str)) {
        return FALSE;
    }
    $str_1 = mb_substr($str, 0, $length, 'utf-8');
    $str_2 = '';
    if (mb_strlen($str, 'UTF-8') > $length) {
        $replace = empty($replace) ? '...' : $replace;
        $str_2 = $replace;
    }
    return $str_1 . $str_2;
}

/**
 * 获取时间戳
 */
function getTimeStr($timestamp = '') {
    if (preg_match("/[0-9]{4}-[0-9]{1,2}-[0-9]{1,2} (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])/i", $timestamp)) {
        list($date, $time) = explode(" ", $timestamp);
        list($year, $month, $day) = explode("-", $date);
        list($hour, $minute, $seconds ) = explode(":", $time);
        $timestamp = gmmktime($hour, $minute, $seconds, $month, $day, $year);
    } else {
        $timestamp = time();
    }
    return $timestamp;
}

/**
 * 获取随机字符串
 */
function getRandStr($length = 3) {

    $str = null;
    $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    $max = strlen($strPol) - 1;

    for ($i = 0; $i < $length; $i++) {
        $str.=$strPol[mt_rand(0, $max)]; //rand($min,$max)生成介于min和max两个数之间的一个随机整数
    }

    return $str;
}

/**
 * 递归删除目录
 */
function delDir($path) {
    //给定的目录不是一个文件夹
    if (!is_dir($path)) {
        return false;
    }
    $fh = opendir($path);
    while (($row = readdir($fh)) !== false) {
        if ($row == '.' || $row == '..') {
            continue;
        }
        if (!is_dir($path . '/' . $row)) {
            unlink($path . '/' . $row);
        }
        deldir($path . '/' . $row);
    }
    closedir($fh);
    if (!rmdir($path)) {
        echo $path . '无权限删除<br>';
    }
    return true;
}

/**
 * @param int $second 前后几秒
 */
function getDateTime($second = 0) {
    return date("Y-m-d H:i:s", strtotime("$second minute"));
}


/**
 * 验证手机号是否正确
 * @param INT $mobile
 */
function isMobile($mobile) {
    if (strlen($mobile) == 11) {
        return preg_match('/^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/', $mobile) ? true : false;
    }
    return FALSE;
}

/**
 * 验证邮箱地址是否正确
 * @param type $email
 * @return boolean
 */
function isEmail($email){
    if(!empty($email)){
        return preg_match('/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/', $email) ? TRUE : FALSE;
    }
    return FALSE;
}

/**
 * 用户名称的检查数字和字母,汉字,下划线
 * @param type $username
 * @return boolean
 */
function isUsername($username){
    if(!empty($username)){
        return preg_match('/[A-Za-z0-9_]+/', $username,$res) ? TRUE : FALSE;
    }
    return FALSE;
}
//判断网络图片是否存在
function imgUlrExists($url = null) {
    if(empty($url)){
        return false;
    }
    $curl = curl_init($url);
    // 不取回数据
    curl_setopt($curl, CURLOPT_NOBODY, true);
    // 发送请求
    $result = curl_exec($curl);
    // 如果请求没有发送失败
    if ($result !== false) {
    // 再检查http响应码是否为200
        return true;
    }
    return false;
}

/**
 * 签名算法 处理函数
 */
function makeSignature($ticket,$noncestr,$timestamp,$url){
    $string1 = "jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$url}";
    return sha1($string1);
}

// 判断是不是手机浏览器 true：是手机，false：不是手机
function isMobileBrow(){
    // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
    {
        return true;
    }
    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (stristr($_SERVER['HTTP_VIA'], "wap"))
    {
        // 找不到为flase,否则为true
        return  true;
    }
    $useragent = strtolower(I('server.HTTP_USER_AGENT'));
    if (isset ($_SERVER['HTTP_USER_AGENT']))
    {
        $clientkeywords = array ('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile');

        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        foreach ($clientkeywords as $value) {
            # code...
            if(false !== strpos($useragent,$value)){
                return true;
            }
        }
    }

    if (isset ($_SERVER['HTTP_ACCEPT'])){
        // 如果只支持wml并且不支持html那一定是移动设备
        // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html'))))
        {
            return true;
        }
    }
    return false;
}
